'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:AdministratorChooseinfoCtrl
 * @description
 * # AdministratorChooseinfoCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('AdministratorChooseinfoCtrl', function($scope, $rootScope, $window, $routeParams, $http, Util, openApi, localStorageService,PagingService) {
        var param = {};
        var selectLists = []; //已经选择了的数组，用来回传

        // 底部款项类型选框添加
        $scope.sheetObj = {};
        var cerkeyObj = {};
        cerkeyObj.sheettitle = "印章类别";
        cerkeyObj.callback = function(item) {
            $scope.certificateType = item.selectTab;
            $scope.search();
        }
        $scope.sheetObj.cerkeyObj = cerkeyObj;
        $scope.showSheet = function(type) {
            var sheetObj = $scope.sheetObj[type]
            $scope.sheetObj.type = type
            $scope.sheetObj.tabList = sheetObj.tabList
            $scope.sheetObj.sheettitle = sheetObj.sheettitle
            $scope.sheetObj.ifshowSheet = true
            $rootScope.showMasker = true
        };
        // -----------------底部款项类型选框添加end-----------------------
        $scope.isSelect = function(index) {
            return $scope.isSelectindex == index
        }
        $scope.choosePeople = function(index) {
            var curItemIndex;
            var curItem;
            curItem = $scope.taskList.items[index]
            curItemIndex = $scope.checkIfExsit(index);
            if (curItemIndex == -1) {
                selectLists.push(curItem)
            } else {
                selectLists.splice(curItemIndex, 1)
            }
            return selectLists;
        };
        $scope.checkIfExsit = function(index) {
            var selecrIndex = -1;
            var pkId;
            pkId = $scope.taskList.items[index].pkId;
            selectLists.forEach(function(item, index) {
                if (item.pkId == pkId) {
                    selecrIndex = index;
                    return selecrIndex;
                }
            });
            return selecrIndex;
        };
        // 搜索
        $scope.search = function() {
            var relatedModule;
            relatedModule = getParamUrl(); //获取借印章或者还印章
            param.filterSql = $scope.certificateType || "";
            param.relatedModule = relatedModule || "";
            $scope.taskList = new PagingService(openApi.getRelpage,param);
        };
        // 根据不同的id请求的列表地址会不同
        var getParamUrl = function() {
            var url, form_id;
            form_id = $routeParams.id;

            if (form_id == "jyz") {
                // 借印章
                url = "sealBorrow_select_seal";
            } else if (form_id == "hyz") {
                // 还印章
                url ="sealReturn_select_seal";
            }
            return url;
        };
        // 获取数据返回
        $scope.submit = function() {
            // 缓存中的数据
            var localSelectLists;
            if (selectLists.length < 1) {
                Util.Toast("请选择");
                return;
            }
            localSelectLists = Util.getSelectLists();
            selectLists = Util.mergeArr(selectLists, localSelectLists,"pkId")
            localStorageService.set("selectLists", selectLists);
            $window.history.back();
        };
        // 获取类别

        $scope.getCerTypeList = function() {
            var param = {};
            param.type = '印章类别';
            Util.getcerTypeList(param, function(data) {
                cerkeyObj.tabList = data.data;
                $scope.showSheet('cerkeyObj');
            });
        }

        // 页面初始化请求数据
        var init = function() {
            $scope.search();
        };
        init();
    });
